/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_guestbook.htm
 *
 * Zhiqim Guestbook is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.example.gbook.action;

import org.zhiqim.example.gbook.dbo.Message;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.GetPostAction;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.onex.IsInteger;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Updater;

/**
 * 管理员回复留言
 *
 * @version v1.0.0 @author zouzhigang 2016-9-1 新建与整理
 */
public class AdminReplyAction extends GetPostAction
{
    @Override
    protected void validate(HttpRequest request)
    {//表单验证
        request.addValidate(new IsInteger("messageId", "请选择一个有效的留言"));
        request.addValidate(new IsLen("messageReply", "回复内容不能为空且不超时500字符", 1, 500));
    }

    @Override
    protected void doGet(HttpRequest request) throws Exception
    {//回复页面
        long messageId = request.getParameterLong("messageId");
        if (messageId == -1)
        {
            request.returnHistory("请选择一个留言");
            return;
        }
        
        Message message = ORM.table().item(Message.class, messageId);
        if (message == null)
        {
            request.returnHistory("请选择一个有效的留言");
            return;
        }
        
        request.setAttribute("message", message);  
    }
    
    @Override
    protected void doPost(HttpRequest request) throws Exception
    {//回复处理
        long messageId = request.getParameterLong("messageId");
        String messageReply = request.getParameter("messageReply");
        
        Updater updater = new Updater();
        updater.addMust("messageId", messageId);
        updater.addField("messageReply", messageReply);
        updater.addField("messageRtime", DateTimes.getDateTimeString());
        
        ORM.table().update(Message.class, updater);
    }
}
